package util;

import com.itextpdf.text.Font;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.*;

import javax.swing.*;
import java.awt.*;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * @author 别人
 */
public class PdfUtils {

    // 字体
    private static BaseFont baseFont = null;

    static {
        try {
            /**
             * 设置字体
             *
             * windows路径字体
             * FONT_TYPE=C:/Windows/fonts/simsun.ttc
             * linux路径字体 宋体 (如果没有这个字体文件，就将windows的字体传上去)
             * FONT_TYPE=/usr/share/fonts/win/simsun.ttc
             */
            //可以用配置文件读取
            //获取配置
            //PropertiesLoader pl = new PropertiesLoader("/config/config.properties");
            //拼接文件web访问路径
            //String FONT_TYPE = pl.getProperty("FONT_TYPE");
            //解决中文问题  幼圆
            baseFont = BaseFont.createFont("C:/Windows/fonts/simsun.ttc,1", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 文档超级  排版
     *
     * @param type 1-标题 2-标题一  3-标题二 4-标题三  5-正文  6-左对齐
     */
    public static Paragraph getFont(int type, String text) {
        Font font = new Font(baseFont);
        if (1 == type) {//1-标题
            font.setSize(16f);
            font.setStyle(Font.BOLD);
        } else if (2 == type) {//2-标题一
            font.setSize(16f);
            font.setStyle(Font.BOLD);
        } else if (3 == type) {//3-标题二
            font.setSize(14f);
            font.setStyle(Font.BOLD);
        } else if (4 == type) {//4-标题三
            font.setSize(14f);
        } else if (5 == type) {//5-正文
            font.setSize(10.5f);
        } else if (6 == type) {//6-左对齐
            font.setSize(10.5f);
        } else {
            font.setSize(10.5f);//默认大小
        }
        //注： 字体必须和 文字一起new
        Paragraph paragraph = new Paragraph(text, font);
        if (1 == type) {
            paragraph.setAlignment(Paragraph.ALIGN_CENTER);//居中
            paragraph.setSpacingBefore(10f);//上间距
            paragraph.setSpacingAfter(10f);//下间距
        } else if (2 == type) {//2-标题一
            paragraph.setAlignment(Element.ALIGN_JUSTIFIED); //默认
            paragraph.setSpacingBefore(2f);//上间距
            paragraph.setSpacingAfter(2f);//下间距
        } else if (3 == type) {
            paragraph.setSpacingBefore(2f);//上间距
            paragraph.setSpacingAfter(1f);//下间距
        } else if (4 == type) {//4-标题三
            //paragraph.setAlignment(Element.ALIGN_RIGHT);//右对齐
            paragraph.setSpacingBefore(2f);//上间距
            paragraph.setSpacingAfter(2f);//下间距
        } else if (5 == type) {
            paragraph.setAlignment(Element.ALIGN_JUSTIFIED);
            paragraph.setFirstLineIndent(24);//首行缩进
            paragraph.setSpacingBefore(1f);//上间距
            paragraph.setSpacingAfter(1f);//下间距
        } else if (6 == type) {//左对齐
            paragraph.setAlignment(Element.ALIGN_LEFT);
            paragraph.setSpacingBefore(1f);//上间距
            paragraph.setSpacingAfter(1f);//下间距
        }
        //paragraph.setIndentationLeft(50);//整体缩进左边
        //paragraph.setFirstLineIndent(40);//首行缩进
        return paragraph;
    }

    //为了使文字向左移动5像素
    private static int interval = -5;
    /**
     * PDF加文字水印
     * @param inputFile        PDF文件输入路径
     * @param outputFile       PDF文件输出路径
     * @param waterMarkName    自定义水印
     */
    private static void waterMarkPdf(String inputFile,String outputFile, String waterMarkName) {
        //根据水印文字长度，更改水印文字大小
        int size = 0;
        if (waterMarkName.length() <= 13) {
            size = 50;
        }else if(waterMarkName.length() > 13 && waterMarkName.length() <= 33) {
            size = 35;
        }else {
            size = 20;
        }
        try {
            PdfReader reader = new PdfReader(inputFile);
            FileOutputStream outputStream = new FileOutputStream(outputFile);
            PdfStamper stamper = new PdfStamper(reader, outputStream);
            //设置文字样式
            BaseFont base = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.EMBEDDED);

            Rectangle pageRect = null;

            PdfGState gs = new PdfGState();
            //设置透明度
            gs.setFillOpacity(0.03f);

            gs.setStrokeOpacity(0.4f);

            int total = reader.getNumberOfPages() + 1;

            JLabel label = new JLabel();

            FontMetrics metrics;

            int textH = 0;

            int textW = 0;
            //设置自定义水印
            label.setText(waterMarkName);

            metrics = label.getFontMetrics(label.getFont());

            textH = metrics.getHeight();

            textW = metrics.stringWidth(label.getText());

            PdfContentByte under = null;

            for (int i = 1; i < total; i++) {

                pageRect = reader.getPageSizeWithRotation(i);

                under = stamper.getOverContent(i);

                under.saveState();

                under.setGState(gs);

                under.beginText();

                under.setFontAndSize(base, size);

                //调整for循环的宽高可更改水印位置，高：textH 宽：textW
                for (int height = interval + textH; height < pageRect.getHeight(); height = height + textH*10) {

                    for (int width = interval + textW; width < pageRect.getWidth() + textW; width = width + textW*5) {
                        //水印文字成21度角倾斜
                        under.showTextAligned(Element.ALIGN_LEFT, waterMarkName, width - textW, height - textH, 21);
                    }
                }
                // 添加水印文字
                under.endText();
            }
            //关闭流
            stamper.close();
            outputStream.close();
            reader.close();

        } catch (Exception e) {
        }
    }


}